package oj_leetcode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SubsetsWithDup {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> tmp = new ArrayList<>();
        Set<Integer> used = new HashSet<>();
        subsetsWithDup(res, tmp, nums, 0, used);
        return res;
    }

    public void subsetsWithDup(List<List<Integer>> res, List<Integer> tmp, int[] nums, int curr, Set<Integer> used)
    {
        res.add(new ArrayList<>(tmp));
        for (int i = curr; i < nums.length; i++) {
            if (used.contains(nums[i])) {
                continue;
            }
            tmp.add(nums[i]);
            subsetsWithDup(res, tmp, nums, i + 1, new HashSet<>(used));
            tmp.remove(tmp.size() - 1);
            used.add(nums[i]);
        }
    }

    public static void main(String[] args) {
        System.out.println(new SubsetsWithDup().subsetsWithDup(new int[]{1, 2, 2}));
    }
}
